<template>
  <el-dialog
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="dialogData.title"
    :visible.sync="isDialog"
    width="600px"
  >
    <el-form
      ref="submitFormRef"
      :model="submitForm"
      :rules="submitFormRules"
      label-position="right"
      label-width="auto"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="submitForm.name"
          clearable
          placeholder="请输入名称"
          type="text"
        ></el-input>
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-slider v-model="submitForm.sort" :max="255" show-input></el-slider>
        <span class="form-tip">越小越靠前</span>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="isLoading.submit"
        type="primary"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from "@/api/providerCategory";

export default {
  name: "Edit",
  props: {
    info: {
      type: Object,
    },
    mode: {
      type: String,
    },
  },
  data() {
    return {
      // 表单提交
      submitForm: {
        parent_id: this.info.parent_id,
        name: "",
        sort: 0,
      },
      // 表单验证
      submitFormRules: {
        parent_id: [
          { required: true, message: "请选择上级分类", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入标题", trigger: "blur" }],
        sort: [{ required: true, message: "请输入权重", trigger: "blur" }],
      },
      // 数据
      dialogData: {
        title: "",
      },
      // 加载状态
      isLoading: {
        submit: false,
      },
      // 是否弹窗
      isDialog: true,
    };
  },
  created() {
    console.log(this.mode, 111);
    switch (this.mode) {
      case "addLevel1":
        this.submitForm.parent_id = 0;
        this.dialogData.title = "添加";
        break;
      case "addLevel2":
        this.submitForm.parent_id = this.info.cate_id;
        this.dialogData.title = "添加子分类" + "(" + this.info.name + ")";
        break;
      case "edit":
        this.dialogData.title = "修改" + "(" + this.info.name + ")";
        this.getData();
        break;
    }
  },
  methods: {
    getData() {
      let dataInfo = this.info;
      this.submitForm = {
        cate_id: dataInfo.cate_id,
        name: dataInfo.name,
        sort: Number(dataInfo.sort),
      };
    },
    // 提交
    handleSubmit() {
      this.$refs.submitFormRef.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.isLoading.submit = true;

        switch (this.mode) {
          case "addLevel1":
            add(this.submitForm)
              .then(() => {
                this.isLoading.submit = false;
                this.$emit("close");
              })
              .catch(() => {
                this.isLoading.submit = false;
              });
            break;
          case "addLevel2":
            add(this.submitForm)
              .then(() => {
                this.isLoading.submit = false;
                this.$emit("close");
              })
              .catch(() => {
                this.isLoading.submit = false;
              });
            break;
          case "edit":
            edit(this.submitForm)
              .then(() => {
                this.isLoading.submit = false;
                this.$emit("close");
              })
              .catch(() => {
                this.isLoading.submit = false;
              });
            break;
        }
      });
    },
    // 关闭
    handleClose() {
      this.$emit("close", "cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea textarea {
  font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}

::v-deep .tox-tinymce {
  min-height: 500px;
}

.image-box {
  width: 100%;
  height: 100%;
}
</style>
